﻿Public Class frmMasterItemCode
    Dim dbBase As New dbMasterTable
    Dim frmCtrl As New FormControl
    Dim dsBase As DataSet
    Dim dsGroup As DataSet
    Dim Var As New varDeclare
    Dim TypeAct As String

    Private Sub frmMasterItemCode_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dsBase = dbBase.getItemCodeList()
        Textbox(False, False)
        dsGroup = dbBase.getGroupItemList()
        AdvTxtGroup.Visible = False
        AdvTxtGroup.Initialize(dsGroup, "Groups ID", txtGroups)
        GridAndFind1.Initialize(dsBase, "Item Code", ToolbarHeader1, Me, True)
        ToolbarHeader1.Iniliaze("Form : Groups Item", Me, Nothing, GridAndFind1.getDataGridView(), "Item Code")
        Dim cmbItem As New List(Of comboItem)
        cmbItem.Add(New comboItem("Active", "A"))
        cmbItem.Add(New comboItem("Inactive", "I"))
        cmbStatus.DataSource = cmbItem
        cmbStatus.DisplayMember = "Text"
        cmbStatus.ValueMember = "Value"
    End Sub

    Private Sub Textbox(st As Boolean, clear As Boolean)
        cmbStatus.Enabled = st
        If (st) Then
            st = False
        Else : st = True
        End If
        txtItemCode.ReadOnly = st
        txtItemCode.BackColor = Color.White
        txtItemDesc.ReadOnly = st
        txtItemDesc.BackColor = Color.White
        txtGroups.ReadOnly = st
        txtGroupsDesc.ReadOnly = st
        GridAndFind1.Enabled = st
        If (clear) Then
            txtItemCode.Text = ""
            txtItemDesc.Text = ""
        End If
        If (TypeAct = "I") Then
            txtItemCode.ReadOnly = False
        Else : txtItemCode.ReadOnly = True
        End If
    End Sub

    Private Sub resetField()
        txtItemCode.BackColor = Color.White
        txtItemDesc.BackColor = Color.White
    End Sub

    Private Sub tsNew_Click(ByVal sender As Object) Handles ToolbarHeader1.tsNewClick
        TypeAct = "I"
        Textbox(True, True)
    End Sub

    Private Sub tsEdit_Click(ByVal sender As Object) Handles ToolbarHeader1.tsEditClick
        TypeAct = "U"
        Textbox(True, False)
    End Sub

    Private Sub tsDelete_Click(ByVal sender As Object) Handles ToolbarHeader1.tsDeleteClick
        If (txtItemCode.Text.Trim() <> "") Then
            TypeAct = "D"
            Dim result As String = dbBase.ItemCodeManagement(TypeAct, txtGroups.Text.Trim(), txtItemCode.Text.Trim(), txtItemDesc.Text.Trim(), cmbStatus.SelectedValue.ToString())
            If (result = "Success") Then
                frmCtrl.openChild(New frmMasterItemCode, Me.ParentForm)
            Else : MessageBox.Show(result)
            End If
        Else : MessageBox.Show("Please Choose User First")
        End If
    End Sub

    Private Sub tsPrint_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsPrintClick
        ''Event 
    End Sub

    Private Sub tsSave_Click(ByVal sender As Object) Handles ToolbarHeader1.tsSaveClick
        Dim Status As Boolean
        If (IsNumeric(txtItemCode.Text.Trim()) = False) Then
            txtItemCode.BackColor = Color.LightPink
            Status = False
        End If
        If (Status) Then
            If (txtItemCode.Text.Trim() <> "" And txtItemDesc.Text.Trim() <> "" And cmbStatus.Text <> "") Then
                Dim result As String = dbBase.ItemCodeManagement(TypeAct, txtGroups.Text.Trim(), txtItemCode.Text.Trim(), txtItemDesc.Text.Trim(), cmbStatus.SelectedValue.ToString())
                If (result = "Success") Then
                    frmCtrl.openChild(New frmMasterItemCode, Me.ParentForm)
                Else : MessageBox.Show(result)
                End If
            Else : MessageBox.Show("Please Fill All Field")
            End If
        End If
    End Sub

    Private Sub tsRefresh_Click(ByVal sender As Object) Handles ToolbarHeader1.tsRefreshClick
        frmCtrl.openChild(New frmMasterItemCode, Me.ParentForm)
    End Sub

    Private Sub tsCancel_Click(ByVal sender As Object) Handles ToolbarHeader1.tsCancelClick
        frmCtrl.openChild(New frmMasterItemCode, Me.ParentForm)
    End Sub

    Private Sub GridCellEnter_Click(ByVal sender As GridAndFind, KeyValue As String) Handles GridAndFind1.GridCellEnter
        Dim dvFind As DataView = New DataView(dsBase.Tables(0))
        dvFind.RowFilter = "[Item Code] = '" & KeyValue & "'"
        txtGroups.Text = dvFind.Item(0).Row("Groups").ToString()
        txtItemCode.Text = dvFind.Item(0).Row("Item Code").ToString()
        txtItemDesc.Text = dvFind.Item(0).Row("Item Desc").ToString()
        cmbStatus.SelectedValue = dvFind.Item(0).Row("Status")
        Dim dvGroup As DataView = New DataView(dsGroup.Tables(0))
        dvGroup.RowFilter = "[Groups ID] = '" & txtGroups.Text & "'"
        txtGroupsDesc.Text = dvGroup.Item(0).Row("Group Desc").ToString()
    End Sub

    Private Sub txtGroups_TextChanged(sender As Object, e As EventArgs) Handles txtGroups.TextChanged
        AdvTxtGroup.Visible = False
        If (txtGroups.Text = "load") Then
            txtGroups.Text = AdvTxtGroup.dsResult.Tables(0).Rows(0)("Groups ID").ToString()
            txtGroupsDesc.Text = AdvTxtGroup.dsResult.Tables(0).Rows(0)("Group Desc").ToString()
        End If
    End Sub

    Private Sub ShowAdvGroups_DoubleClick(sender As Object, e As EventArgs) Handles txtGroups.DoubleClick, txtGroupsDesc.DoubleClick
        AdvTxtGroup.Visible = True
        AdvTxtGroup.BringToFront()
        AdvTxtGroup._Show()
    End Sub
End Class